package org.linlinjava.litemall.admin.job;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.linlinjava.litemall.core.notify.AwsNotifyService;
import org.linlinjava.litemall.core.util.BigDecimalUtil;
import org.linlinjava.litemall.db.domain.LitemallAdmin;
import org.linlinjava.litemall.db.domain.LitemallCashSettlement;
import org.linlinjava.litemall.db.domain.LitemallShop;
import org.linlinjava.litemall.db.domain.LumiereAmountDifference;
import org.linlinjava.litemall.db.service.LitemallAdminService;
import org.linlinjava.litemall.db.service.LitemallCashSettlementService;
import org.linlinjava.litemall.db.service.LitemallShopService;
import org.linlinjava.litemall.db.service.LumiereAmountDifferenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.List;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Component
public class AdminAmountDiffJob {

    @Resource
    private LumiereAmountDifferenceService lumiereAmountDifferenceService;

    @Resource
    private LitemallCashSettlementService cashSettlementService;

    @Resource
    private LitemallAdminService litemallAdminService;

    @Resource
    private LitemallShopService litemallShopService;

    @Autowired
    private AwsNotifyService awsNotifyService;

    // 每小时执行一次的定时任务
    @Scheduled(cron = "0 0 23 * * ?")
    public void syncAmountDifferenceData() {
        LocalDate today = LocalDate.now();
        LocalDateTime startOfDay = today.atStartOfDay();
        LocalDateTime endOfDay = today.atTime(LocalTime.MAX);

        List<LitemallCashSettlement> settlements = cashSettlementService.list(
            Wrappers.lambdaQuery(LitemallCashSettlement.class)
                    .between(LitemallCashSettlement::getAddTime, startOfDay, endOfDay)
        );

        for (LitemallCashSettlement cashSettlement : settlements) {
            LumiereAmountDifference amountDifference = lumiereAmountDifferenceService.getOne(
                Wrappers.lambdaQuery(LumiereAmountDifference.class)
                        .eq(LumiereAmountDifference::getShopId, cashSettlement.getShopId())
                        .eq(LumiereAmountDifference::getEnabled, 1)
            );
            if (BigDecimalUtil.greaterThan(cashSettlement.getDifferenceAmount(),amountDifference.getAmount())) {
                LitemallAdmin admin = litemallAdminService.findById(amountDifference.getAdminId());
                LitemallShop shop = litemallShopService.findById(amountDifference.getShopId());
                awsNotifyService.noticeMail("Store Discrepancy Amount Reminder", "hello, "+ admin.getNickName()+"Store Manager of "+ shop.getName()+", the daily store discrepancy amount is "+cashSettlement.getDifferenceAmount()+", which is greater than the set discrepancy limit of "+amountDifference.getAmount()+". Please check the stores daily count again!", "", admin.getEmail());
            }
        }
    }

}
